<template>
  <div class="order-detail-container">
    <Header
      title="订单详情"
    />
    <div class="status-container">
      <p>{{orderData.Status}}</p>
      <Button
        class="btn pay"
        :loading="payLoading"
        @click="pay"
        type="primary"
        v-if="orderData.Status === '待付款'"
      >付款</Button>
    </div>
    <section class="main-contianer">
      <div class="shop-info">
        <img :src="orderData.StoreIcon" alt="">
        <p class="name">{{orderData.StoreName}}</p>
      </div>
      <div class="goods-list">
        <div class="item" v-for="(item, index) in orderData.GoodsList" :key="index">
          <img :src="item.Image" alt="">
          <p class="name">{{item.Name}}</p>
          <p class="num">x{{item.Quantity}}</p>
          <p class="price"><span>¥</span>{{item.Amount}}</p>
        </div>
      </div>
      <Cell
        v-show="orderData.Shipment === '到店自提'"
        title="配送费"
        :value="`¥${orderData.DeliverFee}`"
      />
      <Cell
        title="在线支付优惠金额"
        v-show="orderData.MinusAmount > 0"
        :value="`-¥${orderData.MinusAmount}`"
      />
      <Cell
        title="红包"
        v-show="orderData.CouponAmount > 0"
        :value="`-¥${orderData.CouponAmount}`"
      />
      <div class="total-price">
        实付¥<span>{{orderData.ReceivableAmount}}</span>
      </div>
      <p class="red-tip" v-show="orderData.IsShowTip">使用红包时，实际支付金额小于1元的按1元支付。​​​</p>
    </section>
    <section class="delivery-container">
      <h1>配送信息</h1>
      <div class="content line">
        <p class="title"  v-if="orderData.Shipment !== '到店自提'">送货地址</p>
        <p class="title"  v-else>{{orderData.Shipment}}</p>
        <div class="value" v-if="orderData.Shipment !== '到店自提'">
          <p class="block">{{orderData.Contact}} {{orderData.Mobile}}</p>
          <p class="block">{{orderData.Address}}</p>
        </div>
      </div>
      <div class="content line" v-if="orderData.DeliveryDate">
        <p class="title">发货日期</p>
        <div class="value">
          <p class="block">{{orderData.DeliveryDate}}</p>
        </div>
      </div>
    </section>
    <section class="other-container">
      <h1>订单信息</h1>
      <div class="content">
        <Cell title="订单号" :value="no" />
        <Cell title="支付方式" value="微信支付" />
        <Cell title="下单时间" :value="orderData.CreateTime.substring(0, 19)" />
        <Cell title="订单备注" :value="orderData.Memo || '无'" />
      </div>
    </section>
    <Button
      v-if="orderData.Status === '待付款'"
      type="primary"
      class="cancel"
      :loading="loading"
      @click="cancelOrder"
    >取消订单</Button>
  </div>
</template>
<script>
import Header from '@/components/Header'
import { Cell, Toast, Button, Dialog } from 'vant';
import { clearCartData } from '../../utils/cart';
export default {
  components: {
    Header,
    Cell,
    Button
  },
  data() {
    return{
      refreshLoading: false,
      no: this.$route.params.no,
      orderData: {
        CreateTime: '',
        GoodsList: []
      },
      payLoading: false,
      payData: {
        sourceNo: this.$route.params.no,
        sourceType: 'StoreOrderPayment'
      },
      loading: false
    }
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        this.refreshLoading = false;
      }, 0);
    },
    cancelOrder() {
      Dialog.confirm({
        title: '是否取消该订单'
      }).then(() => {
        this.loading = true;
        this.$http.order.cancel({
          no: this.no
        }).then(() => {
          Toast('取消成功');
          this.loading = false;
          this.getData();
        }).catch(() => {
          Toast('操作失败，请稍后再试');
          this.loading = false;
        })
      })
    },
    getData() {
      this.$showLoading()
      this.$http.order.detail({
        no: this.no
      }).then(({ result }) => {
        this.$hideLoading();
        if (result.DeliveryDate) {
          result.DeliveryDate = result.DeliveryDate.substring(0, 10);
        }
        this.orderData = result;
      })
    },
    pay() {
      this.payLoading = true;
      const { sourceNo, sourceType } = this.payData;
      this.createWxPay(sourceNo, sourceType);
    },
    createWxPay(sourceNo, sourceType) {
      this.$http.pay
        .create({
          sourceNo,
          sourceType
        })
        .then(({ result }) => {
          this.payLoading = false;
          this.$http.pay.wxPay(result).then((res) => {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
              clearCartData(this.orderData.StoreId);
              Toast.success('支付成功!')
              setTimeout(() => {
                this.getData();
              }, 0);
            } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
              //支付过程中用户取消
              Toast.fail("您取消了支付！");
            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
              //支付失败
              Toast.fail(res.err_desc);
            }
          })
        }).catch((err) => {
          this.payLoading = false;
          Toast(err);
        })
    }
  },
  created() {
    this.getData()
  },
  activated() {
    this.getData()
  }
}
</script>
<style lang="less" scoped>
.order-detail-container{
  padding-bottom: 20px;
  .status-container{
    padding:20px 40px;
    font-size:34px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .pay{
      font-size: 28px;
      height: 50px;
      line-height: 50px;
      background-color: @maincolor;
      width: 120px;
    }
  }
  section{
    background-color: #fff;
    margin: 20px 20px 0;
    h1{
      padding: 20px;
      font-size: 30px;
      .border-bottom(#dedede);
    }
    .content{
      padding: 0 20px;
    }
    .line{
      display: flex;
      justify-content: space-between;
      padding: 20px 20px 10px;
      .value{
        width: 500px;
        .block{
          margin-bottom: 10px;
        }
      }
    }
  }
  .main-contianer{
    padding: 0 20px;
    .shop-info{
      display: flex;
      align-items: center;
      padding: 20px 0;
      font-size: 30px;
      .border-bottom(#dedede);
      img{
        width: 60px;
        height: 60px;
        margin-right: 20px;
      }
    }
    .goods-list{
      .item{
        display: flex;
        align-items: center;
        padding: 20px 0;
        img{
          width: 80px;
          height: 80px;
        }
        .name{
          width: calc(100% - 320px);
          font-size: 28px;
          margin-left: 20px;
        }
        .num,
        .price{
          text-align: right;
          font-size: 28px;
          margin-left: 20px;
          width: 70px;
        }
        .price{
          width: 110px;
          span{
            font-size: 24px;
          }
        }
      }
    }
  }
  .red-tip{
    padding-bottom: 20px;
    font-size: 24px;
    text-align: right;
    color: #999;
  }
  .total-price{
    text-align: right;
    line-height: 60px;
    padding: 20px 0;
    font-size: 30px;
    span{
      font-size: 36px;
    }
  }
  .cancel{
    background-color: #fff;
    color: #333;
    margin: 20px auto 0;
    width: 710px;
    display: block;
  }
}
</style>
<style lang="less">
.order-detail-container{
  .van-cell{
    padding: 0.26667rem 0;
    &:after{
      left: 0;
    }
    .van-cell__title,
    .van-cell__value{
      font-size: 0.37333rem;
    }
    .van-cell__value{
      color: #333;
    }
  }
}
</style>
